package com.raos.kafka.partition;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * kafka入门之分区操作 -- 简单分区执行器实现（二分）
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2021/6/19 22:05
 */
public class SimplePartitioner implements Partitioner {

    public SimplePartitioner() { }

    @Override
    public void configure(Map<String, ?> configs) {
        //
    }

    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        String k = (String) key;
        System.out.println(k);
        if (Integer.parseInt(k) % 2 == 0) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public void close() {
        //
    }

}
